/**
 * Created with JetBrains PhpStorm.
 * User: vander
 * Date: 5/29/13
 * Time: 12:31 AM
 * To change this template use File | Settings | File Templates.
 */
var MyTools = new Object();

MyTools.saveTextPersistent = function(filename, data){
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem){
        fileSystem.root.getFile(filename, {create: true, exclusive:false}, function success(file){
            file.createWriter(function(writer){
                writer.seek(0);
                writer.write(data);
            }, function(err){});
        }, fail);
    });
    function fail(){
        console.log("Read file failed!");
    }
}

MyTools.openTextPersistent = function(filename, done){
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem){
        fileSystem.root.getFile(filename,null, function(fileEntry){
            fileEntry.file(function(file){
                var file_reader = new FileReader();
                file_reader.onloadend = function(evt){
                    done(evt.target.result);
                };
                file_reader.readAsText(file);
            }, fail);
        }, fail);
    }, fail);
    function fail(){
        console.log("Read file failed!");
    }
}